package pe.gob.mpfn.model;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table(name = "diligencia")
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "diligencia")
@RooDbManaged(automaticallyDelete = true)
public class Diligencia {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "int_diligencia_id", columnDefinition = "INT")
    private Integer intDiligenciaId;

	public Integer getIntDiligenciaId() {
        return this.intDiligenciaId;
    }

	public void setIntDiligenciaId(Integer id) {
        this.intDiligenciaId = id;
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Diligencia().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countDiligencias() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Diligencia o", Long.class).getSingleResult();
    }

	public static List<Diligencia> findAllDiligencias() {
        return entityManager().createQuery("SELECT o FROM Diligencia o", Diligencia.class).getResultList();
    }

	public static Diligencia findDiligencia(Integer intDiligenciaId) {
        if (intDiligenciaId == null) return null;
        return entityManager().find(Diligencia.class, intDiligenciaId);
    }

	public static List<Diligencia> findDiligenciaEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Diligencia o", Diligencia.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Diligencia attached = Diligencia.findDiligencia(this.intDiligenciaId);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Diligencia merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Diligencia merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	@OneToMany(mappedBy = "intDiligenciaId")
    private Set<DetalleDiligencia> detalleDiligencias;

	@ManyToOne
    @JoinColumn(name = "int_ubigeo_diligencia_id", referencedColumnName = "int_tabla_id", nullable = false)
    private TablaMaestra intUbigeoDiligenciaId;

	@ManyToOne
    @JoinColumn(name = "str_numero_expediente_id", referencedColumnName = "str_numero_expediente_id", nullable = false)
    private Expediente strNumeroExpedienteId;

	@Column(name = "dat_fecha_inicio", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaInicio;

	@Column(name = "dat_fecha_fin", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date datFechaFin;

	@Column(name = "str_observacion", columnDefinition = "VARCHAR", length = 200)
    @NotNull
    private String strObservacion;

	@Column(name = "chr_estado", columnDefinition = "CHAR")
    @NotNull
    private Character chrEstado;

	public Set<DetalleDiligencia> getDetalleDiligencias() {
        return detalleDiligencias;
    }

	public void setDetalleDiligencias(Set<DetalleDiligencia> detalleDiligencias) {
        this.detalleDiligencias = detalleDiligencias;
    }

	public TablaMaestra getIntUbigeoDiligenciaId() {
        return intUbigeoDiligenciaId;
    }

	public void setIntUbigeoDiligenciaId(TablaMaestra intUbigeoDiligenciaId) {
        this.intUbigeoDiligenciaId = intUbigeoDiligenciaId;
    }

	public Expediente getStrNumeroExpedienteId() {
        return strNumeroExpedienteId;
    }

	public void setStrNumeroExpedienteId(Expediente strNumeroExpedienteId) {
        this.strNumeroExpedienteId = strNumeroExpedienteId;
    }

	public Date getDatFechaInicio() {
        return datFechaInicio;
    }

	public void setDatFechaInicio(Date datFechaInicio) {
        this.datFechaInicio = datFechaInicio;
    }

	public Date getDatFechaFin() {
        return datFechaFin;
    }

	public void setDatFechaFin(Date datFechaFin) {
        this.datFechaFin = datFechaFin;
    }

	public String getStrObservacion() {
        return strObservacion;
    }

	public void setStrObservacion(String strObservacion) {
        this.strObservacion = strObservacion;
    }

	public Character getChrEstado() {
        return chrEstado;
    }

	public void setChrEstado(Character chrEstado) {
        this.chrEstado = chrEstado;
    }
}
